package com.controller.rd;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.DateUtils;
import com.common.http.HttpResult;
import com.model.rd.Program;
import com.model.rd.ProgramMm;
import com.model.rd.ProgramVO;
import com.service.rd.ProgramService;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

/**
 * 方案管理
 */
@Controller("rdProgramcontrollser")
@RequestMapping(value = "/api")
public class ProgramController {

	static final Logger logger = LoggerFactory.getLogger(ProgramController.class);

	@Autowired
	ProgramService programService;

	/**
	 * 查询详情
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/rd/program/selectProInfo", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult selectProInfo(@RequestParam String gramId) {
		return programService.selectProInfo(gramId);
	}

	/**
	 * 查询列表
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/rd/program/selectProgramList", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult selectProgramList(@RequestParam Map<String, Object> map) {
		return programService.selectProgramList(map);
	}

	/**
	 * 保存
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/rd/program/saveProgram", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult saveProgram(@RequestBody ProgramVO purchaseProgramVO) {
		return programService.saveProgram(purchaseProgramVO);
	}
	
	@RequestMapping(value = "/rd/program/oaCheck", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult oaCheck(@RequestBody ProgramVO purchaseProgramVO) {
		return programService.oaCheck(purchaseProgramVO);
	}

	/**
	 * 提交
	 * 
	 * @param map
	 * @return
	 */
//	@RequestMapping(value = "/purchase/program/commitProgram", method = { RequestMethod.POST })
//	@ResponseBody
//	public HttpResult commitProgram(@RequestBody PurchaseProgramVO purchaseProgramVO) {
//		return programService.commitProgram(purchaseProgramVO);
//	}

	/**
	 * 导出
	 * 
	 * @param map
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/rd/program/exportProgram")
	public void exportProgram(@RequestParam Map<String, Object> map, HttpServletResponse response) throws IOException {
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		String fileName = URLEncoder.encode("采购计划-" + DateUtils.getCurrentDate(), "UTF-8");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		programService.exportProgram(map, response);

	}

	/**
	 * 删除
	 * 
	 * @param inqId
	 * @return
	 */
	@RequestMapping(value = "/rd/program/delProgram", method = { RequestMethod.DELETE })
	@ResponseBody
	public HttpResult delInquiry(@RequestParam String gramId) {
		return programService.delProgram(gramId);
	}

	/**
	 * 删除mm
	 * 
	 * @param inqId
	 * @return
	 */
	@RequestMapping(value = "/rd/program/delProgramMm", method = { RequestMethod.DELETE })
	@ResponseBody
	public HttpResult delProgramMm(@RequestParam String gramId, @RequestParam String planId) {
		return programService.delProgramMm(gramId, planId);
	}

	/**
	 * 回退
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/rd/program/backProgram", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult backProgram(@RequestBody Program purchaseProgram) {
		return programService.backProgram(purchaseProgram);
	}
}
